package com.linln.admin.miss.service.impl;

import com.linln.admin.miss.domain.Member;
import com.linln.admin.miss.repository.MemberRepository;
import com.linln.admin.miss.service.MemberService;
import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author 小懒虫
 * @date 2021/10/18
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberRepository memberRepository;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    public Member getById(Long id) {
        return memberRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<Member> getPageList(Example<Member> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return memberRepository.findAll(example, page);
    }

    /**
     * 保存数据
     * @param member 实体对象
     */
    @Override
    public Member save(Member member) {
        return memberRepository.save(member);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return memberRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }

    @Override
    public ResultVo login(Member member) {
        Member rsp = memberRepository.findMemberByCellphone(member.getCellphone());
        if (rsp==null){
            return ResultVoUtil.error("登录失败");
        }
        if (!rsp.getPassword().equals(member.getPassword())){
            return ResultVoUtil.error("密码错误");
        }

        return ResultVoUtil.success(rsp);
    }

    @Override
    public ResultVo loginByCode(String cellphone, String code) {
        if (codeMap.get(cellphone)==null||!codeMap.get(cellphone).equals(code)){
            return ResultVoUtil.error("验证码错误");
        }
        Member rsp = memberRepository.findMemberByCellphone(cellphone);
        if (rsp==null){
            Member req = new Member();
            req.setCellphone(cellphone);
            req.setPassword("123456");
            req.setUsername(cellphone);

            memberRepository.save(req);
            return ResultVoUtil.success(req);
        }
        codeMap.remove(cellphone);

        return ResultVoUtil.success(rsp);
    }

    private static Map<String,String> codeMap = new HashMap<>();
    @Override
    public ResultVo sendCode(String cellphone) {
        String code = getRandomNumCode(6);
        codeMap.put(cellphone, code);
        return ResultVoUtil.success(code);
    }
    public static String getRandomNumCode(int number){
        String codeNum = "";
        int [] numbers = {0,1,2,3,4,5,6,7,8,9};
        Random random = new Random();
        for (int i = 0; i < number; i++) {
            int next = random.nextInt(10000);//目的是产生足够随机的数，避免产生的数字重复率高的问题
//    			System.out.println(next);
            codeNum+=numbers[next%10];
        }
        System.out.println("--------");
        System.out.println(codeNum);

        return codeNum;
    }
}